Ext.onReady(function(){
var yesNoStore = Ext.create('Ext.data.Store', {
	autoLoad:true,
	fields: ['name'],
	proxy: {
		type: 'ajax',
		url: 'getList.action?listId=11',
		reader: {
			type: 'json',
			root: 'itemList',
			totalProperty: 'size'
		}
	}
});

var store = Ext.create('Ext.data.Store', {
	pageSize: 20,
	autoLoad:false,
	groupField: 'requestNo',
	fields: [{name:'orderId'},{name:'requestNo'},{name:'clientUniqueId'},{name:'modelName'},{name:'teUser'},{name:'testPurpose'},{name:'projectNumber'},{name:'project'},{name:'product'},{name:'testContent'},
	{name:'status'},{name:'hthm'},{name:'htnm'},{name:'htlm'},{name:'rthm'},{name:'rtnm'},{name:'rtlm'},{name:'lthm'},{name:'ltnm'},{name:'ltlm'},
	{name:'ignitionTime'},{name:'duration'},{name:'storageTime'},{name:'sample'},{name:'fixture'},{name:'outSourcing'},{name:'testLocation'},{name:'plant'},{name:'payer'}],
	proxy: {
		type: 'ajax',
		url: 'getPipeline.action',
		extraParams:{modelId:getURLParameter('modelId')},
		reader: {
			type: 'json',
			root: 'items',
			totalProperty: 'size'
		}
	}
});

var bbar = new Ext.PagingToolbar({  
	store: store,  
	displayInfo: true,
	prependButtons: true,  
	displayMsg : 'Record {0} ~ {1}, Total: {2}',     
	emptyMsg : "No Record",
	doLoad:function(start){  
	}  
});

pipelineGrid = Ext.create('Ext.grid.Panel',{
		id:'pipelineGrid',
		autoScroll:false,
		enableColumnHide:false,
		enableColumnResize:false,
		sortableColumns:false,
		columnLines:true,
		enableColumnMove:false,
		//multiSelect:true,
		//selModel:Ext.create('Ext.selection.CheckboxModel'),
		height:Ext.getBody().getHeight()-165,
		plugins: Ext.create('Ext.grid.plugin.CellEditing', {
			clicksToEdit: 1,
			listeners:{
				beforeedit:function(obj,eOpts){
					/*if(eOpts.record.data.status!='Green Status'){
						return false;
					}*/
					return true;
				}
			}
		}),
		columns:[{
			text:'Status',
			dataIndex:'status',
			minWidth:60,
			renderer: columnWrap
		},{
			text:'Test Engineer',
			width:95,
			dataIndex:'teUser'
		},{
			text:'TRP No.',
			dataIndex:'requestNo',
			width:95
		},{
			text:'Project',
			columns:[{
				text:'Project Number',
				width:50,
				dataIndex:'projectNumber',
				renderer: columnWrap
			},{
				text:'Project',
				width:50,
				dataIndex:'project',
				renderer: columnWrap
			},{
				text:'Product',
				width:50,
				dataIndex:'product',
				renderer: columnWrap
			}]
		},{
			text:'Test Item',
			width:80,
			dataIndex:'testContent',
			renderer: columnWrap
		},{
			text:'Test Purpose',
			dataIndex:'testPurpose',
			flex:1,
			renderer: columnWrap
		},{
			text:'Test Amount',
			width:60,
			renderer:function(value,cellmeta,record,rowIndex,columnIdx,store){
				return 	'HT:'+(parseInt(record.data.hthm)+parseInt(record.data.htnm)+parseInt(record.data.htlm))+'<br>'+
						'RT:'+(parseInt(record.data.rthm)+parseInt(record.data.rtnm)+parseInt(record.data.rtlm))+'<br>'+
						'LT:'+(parseInt(record.data.lthm)+parseInt(record.data.ltnm)+parseInt(record.data.ltlm));
			}
		},{
			text:'Test condition',
			columns:[{
				text:'Ignition Time(s)',
				width:65,
				dataIndex:'ignitionTime',
				renderer: columnWrap
			},{
				text:'Storage Time(h)',
				width:55,
				dataIndex:'storageTime',
				renderer: columnWrap
			},{
				text:'Duration<br>(day)',
				width:55,
				dataIndex:'duration',
				editor: {
					xtype:'numberfield',
					allowBlank: false
				}
			}]
		},{
			text:'Sample',
			width:50,
			dataIndex:'sample'
		},{
			text:'Fixture',
			width:60,
			dataIndex:'fixture',
			editor:{
				xtype:'combo',
				queryMode: 'local',
				store:yesNoStore,
				displayField: 'name',
				valueField: 'name',
				allowBlank: false
			}
		},{
			text:'Out-<br>Sourcing or Not',
			width:65,
			dataIndex:'outSourcing',
			editor:{
				xtype:'combo',
				queryMode: 'local',
				store:yesNoStore,
				displayField: 'name',
				valueField: 'name',
				allowBlank: false
			}
		},{
			text:'Test Location',
			minWidth:60,
			flex:1,
			dataIndex:'testLocation',
			editor:{
				xtype:'textfield',
				allowBlank: false
			}
		},{
			text:'Action',
			width:60,
			renderer:function(value,cellmeta,record,rowIndex,columnIdx,store){
				if(true){
					return '<a href="javascript:changeStatus('+record.data.orderId+',\''+record.data.clientUniqueId+'\')">Change<br>Status</a>';
				}else{
					
				}
			}
		}],
		store:store,
		bbar:bbar/*,
		viewConfig: {
			listeners:{
				'beforecellmousedown':function(view, cell, cellIdx, record, row, rowIdx, eOpts){
					return false;
				}
			}
		},
		listeners:{
			'itemclick':function(view, record, item, index, e, eOpts){
				if(this.getSelectionModel().isSelected(record)){
					this.getSelectionModel().deselect(record,true);
				}else{
					if(record.data.status=='Green Status'){
						this.getSelectionModel().select(record,true);
					}	
				}	
			}
		}*/
	});
	pipelineGrid.getView().on('afterrender',function(){pipelineGrid.getEl().mask('loading...');});
	pipelineGrid.getView().on('refresh',function(){pipelineGrid.getEl().unmask();});
	store.on('beforeload', function (store, options) {
        var new_params = { status: Ext.getCmp('txtStatus').getValue(), addCriteria: Ext.getCmp('addCriteria').getValue(), addValue:Ext.getCmp('addValue').getValue()};
        Ext.apply(store.proxy.extraParams, new_params);
    });
	store.loadPage(1);
	pipelineGrid.getView().refresh();
	Ext.getCmp('pipelinePnl').add(pipelineGrid);
});
